Nevron Open Vision Documentation
Framework / Data / Data Access
In This Topic
    Data Access
    In This Topic

     

     About Data Access

    The purpose of the NOV Data Access API is to provide a slim wrapper around .NET API for working with relational data. NOV Data Access API is not as complete as the API of .NET, but is more simple, and is portable, meaning that you can expose connections to different relational database from a single-code base, regardless of the platform that you deploy to.

     Create a DB Connection

    Before you can perform any queries or commands towards a database, you need to connect to it. The connection to a database is represented by an instance of the NDbConnection class. The following code snippet tries to open a connection to ODBC server database:

    Open DB Connection
    Copy Code
    // first check whether the specific type of connection is supported
    if (NDbConnection.IsTypeSupported(ENDbConnectionType.Odbc))
    {
        NDbConnection connection = null;
        try
        {
            // try open the connection
            connection = NDbConnection.Open(ENDbConnectionType.Odbc, "DSN=Xtreme");
                           
            // TODO - execute queries and commands
        }
        catch (Exception ex)
        {
            // TODO: report connection or execution error
        }
        finally
        {
            // close the connection
            if (connection != null)
            {
                connection.Close();
            }
        }
    }
    

    The type of connection is specified by the ENDbConnectionType enumeration, which current defines the following entries:

    • Odbc - connection to ODBC.
    • OleDb - connection to OleDb.
    • SqlServer - connection to a SQL server.
    • OracleServer - connection to Oracle server.

    As shown in the example, you must first check whether your environment supports a specific type of connection before trying to open such a connection. Trying to open an unsupported connection type will result in an exception.

    Never forget to close the connection once you have finished using it.

     Executing Db Commands

    Once you have successfully opened a connection you can generally execute three types of commands towards it:

    Method Description

    NDbDataReader ExecuteReader(string sqlQuery)

    Executes the query against this connection, and returns a NDbDataReader.

    int ExecuteNonQuery(string sqlStatement)

    Executes a SQL statement against this connection and returns the number of affected rows.

    object ExecuteScalar(string command)

    Executes the query and returns the first column of the first row in the result set returned by the query. All other columns and rows are ignored.

    The following code example executes a SQL query and reads the results:

    Reading SQL Query
    Copy Code
    // TODO - execute queries and commands
    NDbDataReader reader = connection.ExecuteReader("SELECT * FROM SALES");
    while (reader.Read())
    {
        string name = Convert.ToString(reader.GetRowValue(0));
        double price = Convert.ToDouble(reader.GetRowValue(1));
    }
    

    You can also make a NMemoryDataTable from a NDbDataReader like this:

    Creating a NMemoryDataTable from a NDbDataReader
    Copy Code
    NDbDataReader reader = connection.ExecuteReader("SELECT * FROM SALES");
    NMemoryDataTable dataTable = new NMemoryDataTable(reader);